package com.csj.cloud.common;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.springframework.util.ObjectUtils;

/**
 * @Author:ChengshaoJie
 * @Description:
 * @Date:Created in 下午 3:08 2019/3/22 0022
 */
public class MPGenerator {

    private static String OutputDir = "D:/myProjects/mall-moring/";
    //    private static String OutputDir = "D:/myProjects/cloud/src/main/java";
    private static String url = "jdbc:mysql://127.0.0.1:3306/mall-moring?useUnicode=true&characterEncoding=utf-8&useSSL=false";
    private static String userName = "root";
    private static String password = "root";
    private static String author = "chengshaojie";

    public static void main(String[] args) {
        String[] foleders = {"mall-common/src/main/java", "mall-core/src/main/java"};
        //文件夹、用户名、表名、表前缀
        dealDb(foleders, "chengshaojie", "cart_item", new String[]{"m_"});
    }

    private static void dealDb(String[] folders, String author, String tableName, String[] tablePrefix) {
        try {

            for (String s : folders) {
                s = OutputDir + s;
                // 代码生成器
                AutoGenerator mpg = new AutoGenerator();

                // 策略配置
                StrategyConfig strategy = new StrategyConfig();
                String[] tables = new String[]{tableName};
                // 需要生成的表
                strategy.setInclude(tables);
                // 表名生成策略
                strategy.setNaming(NamingStrategy.underline_to_camel);
                strategy.setEntityLombokModel(true);
                //表前缀
                if (!ObjectUtils.isEmpty(tablePrefix)) {
                    strategy.setTablePrefix(tablePrefix);
                }
                mpg.setStrategy(strategy);

                // 全局配置
                GlobalConfig gc = new GlobalConfig();
                gc.setOutputDir(s);
                gc.setOpen(false);
                gc.setAuthor(author);
                gc.setFileOverride(false);
                gc.setControllerName("");
                gc.setServiceName("%sService");
                gc.setServiceImplName("%sServiceImpl");
                gc.setMapperName("%sMapper");
                gc.setXmlName("%sMapper");
                gc.setBaseResultMap(true);
                //二级缓存关
                gc.setEnableCache(false);
                mpg.setGlobalConfig(gc);

                // 数据源配置
                DataSourceConfig dsc = new DataSourceConfig();
                dsc.setDbType(DbType.MYSQL);
                dsc.setUrl(url);
                dsc.setDriverName("com.mysql.jdbc.Driver");
                dsc.setUsername(userName);
                dsc.setPassword(password);
                mpg.setDataSource(dsc);

                if(s.indexOf("core") > -1) {
                    // 包配置
                    PackageConfig pc = new PackageConfig();
                    pc.setParent("com.csj.cloud.core");
                    pc.setService("impl");
                    pc.setServiceImpl("impl");
                    mpg.setPackageInfo(pc);
                    TemplateConfig tc = new TemplateConfig();
                    tc.setController(null);
//                    tc.setService(null);
//                    tc.setServiceImpl(null);
                    tc.setMapper(null);
                    tc.setXml(null);
                    tc.setEntity(null);
                    mpg.setTemplate(tc);
                    mpg.execute();
                }

                if (s.indexOf("common") > -1) {
                    // 包配置
                    PackageConfig pc = new PackageConfig();
                    pc.setEntity("entity");
                    pc.setParent("com.csj.cloud.common");
                    pc.setXml("dao.mapper");
                    pc.setMapper("dao");
                    mpg.setPackageInfo(pc);
                    TemplateConfig tc = new TemplateConfig();
                    tc.setController(null);
                    tc.setService(null);
                    tc.setServiceImpl(null);
                    mpg.setTemplate(tc);
                    mpg.execute();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
